package com.tanhua.common.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.QuestionMapper;
import com.tanhua.common.pojo.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    public Question queryQuestion(Long userId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        Question question = questionMapper.selectOne(queryWrapper);
        return question;
    }

    /**
     * 保存陌生人问题
     * @param userId
     * @param content
     */
    public void save(Long userId, String content) {
        Question question = this.queryQuestion(userId);
        //修改
        if(null != question){
            question.setTxt(content);
            this.questionMapper.updateById(question);
        }else {
            //新增
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            question.setCreated(new Date());
            question.setUpdated(question.getCreated());
            this.questionMapper.insert(question);
        }
    }
}
